package service.impl;

import com.aliyun.oss.OSS;
import common.R;
import mapper.BaseMapper;
import mapper.SingerMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import pojo.Singer;
import pojo.SingerRequest;
import pojo.Song;
import pojo.SongRequest;
import service.SingerService;

import java.io.IOException;
import java.util.Date;

public abstract class SingerServiceImpl implements SingerService {

    @Override
    public R addSinger(SingerRequest addSingerRequest) {
        BaseMapper<Object> singerMapper = null;
        Singer singer = new Singer();
        BeanUtils.copyProperties(addSingerRequest, singer);
        //给出默认头像,后续管理员可以在线修改
        String pic = "https://java-pp.oss-cn-hangzhou.aliyuncs.com/img/singerPic/user.jpg";
        singer.setPic(pic);
        if (singerMapper.insert(singer) > 0) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    @Override
    public R deleteSinger(Integer id) {
        BaseMapper<Object> singerMapper = null;
        if (singerMapper.deleteById(id) > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @Override
    public R updateSingerMsg(SingerRequest updateSingerRequest) {
        BaseMapper<Object> singerMapper = null;
        Singer singer = new Singer();
        BeanUtils.copyProperties(updateSingerRequest, singer);
        if (singerMapper.updateById(singer) > 0) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }

    @Override
    public R addSong(SongRequest addSongRequest, MultipartFile mpfile) {
        Song song = new Song();
        BaseMapper<Object> songMapper = null;
        BeanUtils.copyProperties(addSongRequest, song);
        String pic = "https://java-pp.oss-cn-hangzhou.aliyuncs.com/img/songPic/tubiao.jpg";
        String fileName = mpfile.getOriginalFilename();
        String storeUrlPath = "song";
        String uploadFileUrl;
        String oos = null;
        try {
            uploadFileUrl = oss.uploadFile(storeUrlPath, fileName, mpfile);
        } catch (IOException e) {
            return R.fatal("上传失败" + e.getMessage());
        }
        song.setCreateTime(new Date());
        song.setUpdateTime(new Date());
        song.setPic(pic);
        song.setUrl(uploadFileUrl);
        if (songMapper.insert(song) > 0) {
            return R.success("上传成功", storeUrlPath);
        } else {
            return R.error("上传失败");
        }
    }



}
